%%%% -*- Erlang -*-

Nonterminals 
functions function statements statement s instruction state direction condition.

Terminals 
%%%main 
'(' ')' '{' '}' '?' ':' ';' ',' '&' '_'
string 
ahead food sense pickup move flip drop turn
here left_ahead right_ahead foe friend_with_food foe_with_food
rock foe_marker home foe_home

number goto. 


Rootsymbol functions.

functions -> function : '$1'.
functions -> function functions : {'$1', '$2'}.


%%%function -> main '(' ')' '{' statements '}' : {main, '$5'}.
function -> string '(' ')' '{' statements '}' : {function, v('$1'), '$5'}.


s -> statement : '$1'.
s -> string ':' statement : {label, 
				      case '$1'of {_, _, A} -> A end, '$3'}.
s -> '$empty'.
     

statements -> s : '$1'.
% statements -> statements ';' s : {'$1', '$3'}. % flatten '$1' ???
statements -> s ';' statements : {'$1', '$3'}. % flatten '$1' ???


statement -> instruction : '$1'.
statement -> goto string : {goto, v('$2')}.
statement -> '(' instruction ')' '?' statement ':' statement : {branch, '$2', '$5', '$7'}.


instruction -> sense '(' direction ',' condition ')' : {sense, '$3', '$5'}.
instruction -> marker '(' direction ',' number ')' : {marker, '$3', v(number)}.

instruction -> pickup : '$1'.

instruction -> move '(' state ',' state ')' : 
		   {move,
		    '$3'
		    %% 		    case '$3' of
		    %% 			n -> n;
		    %% 			{_, _, T} -> T
		    %% 		    end
		    , '$5'}.

%% instruction -> move '(' '_' string ')' : [   ].
%% instruction -> move '(' '_' '&' string ')' : [   ].
instruction -> move   : move.


instruction -> flip   : flip.
instruction -> drop   : drop.
instruction -> turn   : turn.

%% call
instruction -> string '(' ')' : v('$1').


state -> '_' : n.
state -> '&' string : 
	     case '$2' of {_, _, A} ->A end.
state -> string : 
	     case '$1' of {_, _, A} ->A end.


direction -> here : '$1'.
direction -> ahead : '$1'.
direction -> left_ahead : '$1'.
direction -> right_ahead : '$1'.

condition -> food : '$1'.
condition -> foe : '$1'.
condition -> friend_with_food : '$1'.
condition -> foe_with_food : '$1'.
condition -> rock : '$1'.
condition -> foe_marker : '$1'.
condition -> home : '$1'.
condition -> foe_home : '$1'.



Erlang code.

v({_, _, A}) ->
    A.




